package JCT_Chapter_03;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Logger;
/**
 * This program shows how to read in and put out file
 */

public class FileInAndOut {
    private static Logger logger=Logger.getLogger(FileInAndOut.class.toString());
    public static void main(String[] args) throws IOException {

        //获得程序当前路径
        String Location = System.getProperty("user.dir");
        System.out.println(Location);

        //读取文件
        //创建一个File对象
        //File file = new File("D:/PracticeJava/resource/myfile.txt");
        File file = new File(".\\resource\\myfile.txt");
        //创建一个Scanner对象
        Scanner input = new Scanner (file,"UTF-8");
        //输出读取文件的内容
        //读取下一个字符
        String firstname = input.next();
        System.out.println(firstname);
        //读取下一行
        String mi = input.nextLine();
        System.out.println(mi);
        //读取下一行
        String lastname = input.nextLine();
        System.out.println(lastname);
        //关闭输入文件，没有必要但是可以释放资源
        input.close();

        //输出至一个新文件
        logger.info("输出至一个新文件");
        File newfile = new File("D:/PracticeJava/resource/yourfile.txt");
        PrintWriter output = new PrintWriter(newfile);
        output.print("John Smith ");
        output.println(90);
        output.print("Eric Jones ");
        output.println(85);
        output.close();
//        //删除文件
//        if(newfile.exists()&&newfile.isFile()) {
//            newfile.delete();
//            System.out.println("删除单个文件" + "yourfile" + "成功！" );
//        }
    }
}
